package com.campusrecruitment.controller;

import com.campusrecruitment.common.JsonUtils;
import com.campusrecruitment.common.R;
import com.campusrecruitment.pojo.employer;
import com.campusrecruitment.pojo.recruitMessage;
import com.campusrecruitment.pojo.resume;
import com.campusrecruitment.pojo.resumeStatus;
import com.campusrecruitment.service.resumeService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 单位简历管理
 */
@Slf4j
@RestController
@RequestMapping("/candidates")
public class candidatesController {

    private ObjectMapper mapper = new ObjectMapper();

    /**
     * 查询全部
     *
     * @return
     */
    @PostMapping("/list")
    public R<List<resume>> queryResume(@RequestBody resume resume) throws JsonProcessingException {
        String employerName = resume.getEmployerName();

        //初始化json工具
        JsonUtils jsonUtils = new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\candidates.json"));
        String jsonList = jsonUtils.readJson(String.class);

        //读出所有应聘信息
        List<resume> resumes = mapper.readValue(jsonList, new TypeReference<List<resume>>() {
        });
        //使用集合得到该应聘数据
        List<resume> list = new ArrayList<>();
        for (resume r : resumes
        ) {
            if (r.getEmployerName().equals(employerName)) {
                list.add(r);
            }
        }

        return R.success(list);
    }

    /**
     * 根据岗位名查询
     * @param resume
     * @return
     */
    @PostMapping
    public R<List<resume>> queryRecruit(@RequestBody resume resume) throws JsonProcessingException {
        String employerName = resume.getEmployerName();
        String position = resume.getPosition();

        //初始化json工具
        JsonUtils jsonUtils = new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\candidates.json"));
        String jsonList = jsonUtils.readJson(String.class);

        //读出所有应聘信息
        List<resume> resumes = mapper.readValue(jsonList, new TypeReference<List<resume>>() {
        });
        //使用集合得到该应聘数据
        List<resume> list = new ArrayList<>();
        for (resume r : resumes
        ) {
            if (r.getEmployerName().equals(employerName)
                    && r.getPosition().equals(position)
            ) {
                list.add(r);
            }
        }
        return R.success(list);
    }

    /**
     * /**
     * 审核通过  修改resumeStatus中的状态信息，删除应聘信息，修改招聘信息的数量
     * @param resumeStatus
     * @return
     */
    @PostMapping("/accept")
    public R<String> accept(@RequestBody resumeStatus resumeStatus) throws JsonProcessingException {
        //1，修改resumeStatus表中的状态信息
        String employerName = resumeStatus.getEmployerName();
        String position = resumeStatus.getPosition();
        String studentName = resumeStatus.getName();

        //初始化json工具
        JsonUtils jsonUtils = new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\resumeStatus.json"));
        String jsonList = jsonUtils.readJson(String.class);

        //读出所有应聘状态
        List<resumeStatus> resumeStatuses = mapper.readValue(jsonList, new TypeReference<List<resumeStatus>>() {});

        for (int i = 0; i < resumeStatuses.size(); i++
        ) {
            if (resumeStatuses.get(i).getEmployerName().equals(employerName)
                    && resumeStatuses.get(i).getPosition().equals(position)
                    && resumeStatuses.get(i).getName().equals(studentName)
            ) {
                resumeStatuses.get(i).setStatus(resumeStatus.getStatus());
                break;
            }
        }
        String jsonString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(resumeStatuses);
        jsonUtils.writeJson(jsonString);


        //2，删除candidates中应聘信息
        //初始化json工具
        JsonUtils jsonUtils2 = new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\candidates.json"));
        String jsonList2 = jsonUtils2.readJson(String.class);

        //读出所有应聘简历
        List<resume> resumes = mapper.readValue(jsonList2, new TypeReference<List<resume>>() {});
        for (int i = 0; i < resumes.size(); i++
        ) {
            if (resumes.get(i).getEmployerName().equals(employerName)
                    && resumes.get(i).getPosition().equals(position)
                    && resumes.get(i).getName().equals(studentName)
            ) {
                resumes.remove(i);
                break;
            }
        }
        String jsonString2 = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(resumes);
        jsonUtils2.writeJson(jsonString2);

        //3，修改招聘信息的岗位数量
        //初始化json工具
        JsonUtils jsonUtils3=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\recruitMessage.json"));
        String jsonList3=jsonUtils3.readJson(String.class);
        //读出所有招聘信息
        List<recruitMessage> recruitMessages= mapper.readValue(jsonList3,new TypeReference<List<recruitMessage>>(){});

        for (int i=0;i<recruitMessages.size();i++
        ) {
            if (recruitMessages.get(i).getEmployerName().equals(resumeStatus.getEmployerName())
                    &&recruitMessages.get(i).getName().equals(resumeStatus.getPosition())){
                //更新该用户
                recruitMessages.get(i).setSpareNumber(recruitMessages.get(i).getSpareNumber()-1);
                break;
            }
        }

        String jsonString3 = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(recruitMessages);
        jsonUtils3.writeJson(jsonString3);

        //修改状态信息
        return R.success("已通过");
    }


     /**
     * 接收学生简历
      * 1,   存入对应简历状态表中，并且简历状态为审核中
      * 2，从该学生简历中找到对应简历
      * 3，添加到应聘信息
     * @param resume
     * @return
     */
    @PostMapping("/send")
    public R<String> accept(@RequestBody resume resume) throws JsonProcessingException {
        String employerName=resume.getEmployerName();
        String studentName=resume.getName();
        String position=resume.getPosition();
        //1,,存入对应简历状态表中，并且简历状态为审核中
        //初始化json工具
        JsonUtils jsonUtils1 = new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\resumeStatus.json"));
        String jsonList1 = jsonUtils1.readJson(String.class);
        List<resumeStatus> resumeStatuses = mapper.readValue(jsonList1, new TypeReference<List<resumeStatus>>() {});

        //设置对应的简历状态对象
        resumeStatus r=new resumeStatus();
        r.setStatus("审核中");
        r.setName(studentName);
        r.setEmployerName(employerName);
        r.setPosition(position);
        LocalDate date = LocalDate.now(); // get the current date
        r.setTime(date.toString());

        resumeStatuses.add(r);

        String jsonString1 = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(resumeStatuses);
        jsonUtils1.writeJson(jsonString1);

        //2，简历数据
        JsonUtils jsonUtils2 = new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\resume.json"));
        String jsonList2 = jsonUtils2.readJson(String.class);
        List<resume> resumes2 = mapper.readValue(jsonList2, new TypeReference<List<resume>>() {});

        //2,从该学生简历中找到对应简历,添加该简历到应聘信息表中
        JsonUtils jsonUtils3 = new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\candidates.json"));
        String jsonList3 = jsonUtils3.readJson(String.class);
        List<resume> resumes3 = mapper.readValue(jsonList3, new TypeReference<List<resume>>() {});
        for (int i=0;i<resumes2.size();i++
        ) {
            if(resumes2.get(i).getPosition().equals(position)&&
                    resumes2.get(i).getName().equals(studentName)){
                //学生的简历信息不包含employName，而应聘信息简历包含，需要设置对应的employName
                resumes2.get(i).setEmployerName(employerName);
                resumes3.add(resumes2.get(i));
            }
        }
        String jsonString3 = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(resumes3);
        jsonUtils3.writeJson(jsonString3);

        //需要添加状态信息为审核中
        return R.success("接收简历成功！");
    }


    /**
     * 拒绝并删除  修改resumeStatus中的状态信息，删除应聘信息
     * @param resumeStatus
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestBody resumeStatus resumeStatus) throws JsonProcessingException {
        //1，修改resumeStatus表中的状态信息
        String employerName = resumeStatus.getEmployerName();
        String position = resumeStatus.getPosition();
        String studentName = resumeStatus.getName();

        //初始化json工具
        JsonUtils jsonUtils = new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\resumeStatus.json"));
        String jsonList = jsonUtils.readJson(String.class);

        //读出所有应聘状态
        List<resumeStatus> resumeStatuses = mapper.readValue(jsonList, new TypeReference<List<resumeStatus>>() {});

        for (int i = 0; i < resumeStatuses.size(); i++
        ) {
            if (resumeStatuses.get(i).getEmployerName().equals(employerName)
                    && resumeStatuses.get(i).getPosition().equals(position)
                    && resumeStatuses.get(i).getName().equals(studentName)
            ) {
                resumeStatuses.get(i).setStatus(resumeStatus.getStatus());
            }
        }
        String jsonString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(resumeStatuses);
        jsonUtils.writeJson(jsonString);


        //2，删除candidates中对应应聘信息
        //初始化json工具
        JsonUtils jsonUtils2 = new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\candidates.json"));
        String jsonList2 = jsonUtils2.readJson(String.class);

        //读出所有应聘简历
        List<resume> resumes = mapper.readValue(jsonList2, new TypeReference<List<resume>>() {});
        for (int i = 0; i < resumes.size(); i++
        ) {
            if (resumes.get(i).getEmployerName().equals(employerName)
                    && resumes.get(i).getPosition().equals(position)
                    && resumes.get(i).getName().equals(studentName)
            ) {
                resumes.remove(i);
                break;
            }
        }
        String jsonString2 = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(resumes);
        jsonUtils2.writeJson(jsonString2);
        return R.error("审核失败");
    }

}
